package com.crecheng.shop.controller;


import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.UserData;
import com.crecheng.shop.Manage.UserManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.rmi.server.UID;
import java.util.Objects;

@Controller
public class UserController {
    @ResponseBody
    @RequestMapping("/user/regist")
    String userRegist(HttpServletResponse response,
                      HttpServletRequest request,
                      @RequestParam(value = "userName",defaultValue = "")String userName,
                      @RequestParam(value = "password",defaultValue = "")String password,
                      @RequestParam(value = "phone",defaultValue = "-1")BigInteger phone,
                      @RequestParam(value = "email",defaultValue = "")String email){

        if(userName.trim().length()<1)
            return MyMessage.toJson("用户名为空！");
        if(password.trim().length()<1)
            return MyMessage.toJson("密码为空");
        if(email.trim().length()<1)
            return MyMessage.toJson("邮箱为空");

        UserData data=new UserData();
        data.set(userName,password,phone,email);
        if(UserManage.add(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }
    @ResponseBody
    @RequestMapping("/user/login")
    String userLogin(@RequestParam(value = "userName",defaultValue = "")String userName,
                     @RequestParam(value = "password",defaultValue = "")String password){
        if(userName.trim().length()<1)
            return MyMessage.toJson("用户名为空！");
        if(password.trim().length()<1)
            return MyMessage.toJson("密码为空");

        UserData x=UserManage.getByName(userName);
        var password1 = x.getPassword();
        System.out.println(password1);
        if (Objects.equals(password1, password)){
            System.out.println("登录成功");
            return new MyMessage(1,"success",x).json();
        }
        else{
            System.out.println("error");
            return MyMessage.defaultSqlNotFoundError;
        }

    }

    @ResponseBody
    @RequestMapping("/user/get")
    String userget(@RequestParam(value = "id",defaultValue = "")int id){
        if (id<1){
            return MyMessage.defaultSqlError;
        }else
            return JSON.toJSONString(UserManage.get(id));
    }


}

